import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    list: [],
    // goodsCount: 1
  },
  mutations: {
    // 获取商品数据
    List(state, res) {
      state.list = res
    },
    // 加减和改值
    val(state, { id, count }) {
      state.list[state.list.findIndex(item => item.id === id)].goods_count = count
    },
    // input状态
    isSelect(state, {id, val}) {
      state.list[state.list.findIndex(item => item.id === id)].goods_state = val
    },
    selectAll(state, val) {
      state.list.filter(item => item.goods_state = val)
    }
  },
  actions: {
    isList({ commit }, res) {
      commit('List', res)
    }
  },
  getters: {
     // 获取已选中状态，返回一个数组
     count(state) {
      return state.list.filter(item => item.goods_state);
    },
  }
})

export default store